checkLogin();
var member_info = getCache(login_key);
var url = index_goods_list;
var goods_category_id = 0;
// var page = 1;
// var is_more = true;
// var minNum = 4;//最小数据长度
// var dataNum = 0;
// var scroll_height = 0;

show_notice();

getSliders();

scorllInit();

// addItems();

// mui.init({
//     pullRefresh : {
//         container:'#scroll',//待刷新区域标识，querySelector能定位的css选择器均可，比如：id、.class等
//         up : {
//             height:50,//可选.默认50.触发上拉加载拖动距离
//             auto:false,//可选,默认false.自动上拉加载一次
//             contentrefresh : "正在加载...",//可选，正在加载状态时，上拉加载控件上显示的标题内容
//             contentnomore:'没有更多数据了',//可选，请求完毕若没有更多数据时显示的提醒内容；
//             callback :addItems //必选，刷新函数，根据具体业务来编写，比如通过ajax从服务器获取新数据；
//         }
//     }
// });

// function addItems(gc_id) {
//     var category_id = gc_id || goods_category_id;
//     var success = function (res) {
//         var goods_list = res.data.goods_list;
//         curPage = page;
//         if (goods_list.next_page_url) {
//             page += 1;
//             mui("#scroll").pullRefresh().endPullupToRefresh(false);
//         } else {
//             is_more = false;
//             mui("#scroll").pullRefresh().endPullupToRefresh(true);
//         }
//         dataNum = res.data.goods_list.data.length;
//         var html = template('fenye_list_data', goods_list);
//         var str = html.replace(/[\r\n]/g, "").replace(/\ +/g, "");
//         if (page == 1 && str == '') {
//             html = '<div style="align-content: center;height: '+scroll_height+';padding-top: 55%;text-align: center;font-size: 20px;color: lightgray;">空空如也</div>';
//         }
//         $('#fenye_list').append(html);
//         //如果当前页是第一页且数据量不够滚动加载页面高度则结束上拉加载
//         if (page == 1 && minNum > dataNum) {
//             mui("#scroll").pullRefresh().endPullupToRefresh(true);
//         }
//     }
//     if (is_more) {
//         getData(url, combineParams({page:page,category_id:category_id}), success);
//     }
// }

//分页参数设置
fenye_options.url = url;
fenye_options.scrollHeight = scroll_height;
fenye_options.dataName = 'goods_list';
fenye_options.params = {category_id:goods_category_id};
// fenye();


mui('#segmentedControl').on('tap','a',function(e){
    window.top.location.href=this.href;
    var gc_id = parseInt(this.getAttribute('data'));
    if (goods_category_id == gc_id) {
        return;
    }
    goods_category_id = gc_id;
    page = 1;
    is_more = true;
    $('#fenye_list').empty();
    mui("#scroll").pullRefresh().enablePullupToRefresh(); //启用上拉刷新
    mui('#scroll').pullRefresh().scrollTo(0, 0, 100); //滚动到顶部
    mui('#scroll').pullRefresh().refresh(true); //重置上拉
    // mui("#scroll").pullRefresh().endPullupToRefresh(false); //开启加载
    fenye_options.params = {category_id:goods_category_id}
    // fenye();
    // addItems(goods_category_id);
});



function scorllInit() {
    var info = getScreen();
    var slider_height = parseInt($(".mui-slider").height());
    var category_height = parseInt($("#segmentedControl").height());
    var bar_height = parseInt($(".mui-bar").height());
    scroll_height = info[1] - slider_height - category_height - bar_height + 'px';
    mui('.mui-control-content').each(function() {
        this.style.height = scroll_height;
    });
    mui('.mui-scroll-wrapper').each(function(k, v) {
        mui(v).scroll({
            deceleration: (mui.os.ios ? 0.003 : 0.0006),
            indicators: false  //隐藏一条滚动条 增大减速系数。。。
        });
    });
}


function show_notice() {
    var success = function (res) {
        var msg = '';
        var status = 0;
        if (typeof(res.data) == 'object') {
            var flag = false;
            for (var i in res.data) {
                flag = true;
                break;
            }
            if (flag) {
                msg = res.data.msg;
                status = res.data.status;
            }
        }
        var html = template('notice_data', [status,msg]);     //order 模板id
        $('#notice').append(html);
    };
    getData(shop_notice, comParams(), success);
}

// 轮播图
function getSliders() {
    var success = function (res) {
        var data = [];
        if (res.data.shop_list.length > 0) {
            data = res.data.shop_list[0];
        }
        template.defaults.imports.slider_nums = data.sliders.length - 1;
        var html = template('slider_list_data', data);     //order 模板id
        $('#sliders').append(html);
        var gallery = mui('.mui-slider');
        gallery.slider({
            interval:3000//自动轮播周期，若为0则不自动播放，默认为0；
        });
    };
    getData(shop_index, combineParams({aid:aid}), success);
}
//去结算
var onTab = function (event) {
    var cartList = wx.getStorageSync('cart');
    var cartListLength = 0;
    if (!cartList) {
        cartListLength = 0;
    }
    for (var i in cartList) {
        if (cartList[i]) {
            cartListLength++;
        }
    }
    var showPage = 0;
    if (cartListLength) {
        showPage = 1;
    }
    wx.navigateTo({
        url: "cart/cart?showPage=" + showPage
    })
}
var caculator = function () {
    var cartData = wx.getStorageSync('cart');
    var currentNum = 0;
    for (var i in cartData) {
        if (cartData[i]) {
            currentNum++;
        }
        this.setData({
            buyNumber: currentNum
        });
    }
}
